package com.so206.service.impl;

import com.so206.mapper.*;
import com.so206.mapper.ext.SystemCountMapper;
import com.so206.po.*;
import com.so206.po.ext.ConnCountModel;
import com.so206.po.ext.CountRet;
import com.so206.service.SystemCountService;
import com.so206.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SystemCountServiceImpl implements SystemCountService {

    @Autowired
    private SystemCountMapper systemCountMapper;

    @Autowired
    private EpayConfigMapper epayConfigMapper;

    @Autowired
    private CodepayConfigMapper codepayConfigMapper;

    @Autowired
    private WxpayConfigMapper wxpayConfigMapper;

    @Autowired
    private QqpayConfigMapper qqpayConfigMapper;

    @Autowired
    private AlipayConfigMapper alipayConfigMapper;

    @Autowired
    private SystemOrderMapper systemOrderMapper;

    @Override
    public int countUsers() {
        return systemCountMapper.countUser();
    }

    @Override
    public int countOrders(Integer status, Long uid) {
        return systemCountMapper.countOrder(status, uid);
    }

    @Override
    public BigDecimal findUserSettAllCount(Long uid) {
        return systemCountMapper.countSettAll(uid);
    }

    @Override
    public BigDecimal findSettAllCount() {
        return systemCountMapper.countSettAll2();
    }

    @Override
    public BigDecimal todayInMoney(Long uid, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.todayInMoney(uid, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal todayChangeMoneySett(Long uid, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {

        }
        return systemCountMapper.todayChangeMoneySett(uid, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal todaySettMoney(Long uid, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.todaySettMoney(uid, null, null);
    }

    @Override
    public BigDecimal todayWaitSettMoney(Long uid, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.todayWaitSettMoney(uid, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public Integer todayCreateOrder(Long uid, Integer status, String timelike, Integer otype, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.todayCreateOrder(uid, status, utils.getStartTime(), utils.getEndTime(), otype);
    }

    @Override
    public BigDecimal todayOrderSettMoneyCount(Long uid, Integer status, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.todayOrderSettMoneyCount(uid, status, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal todayOrderMoneyCount(Long uid, Integer status, String timelike, Integer pay_type, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.todayOrderMoneyCount(uid, status, utils.getStartTime(), utils.getEndTime(), pay_type);
    }

    @Override
    public BigDecimal ptMoneyCountByType(Integer ptype, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.ptMoneyCountByType(ptype, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal ptMoneyInByChangeSett(String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.ptMoneyInByChangeSett(utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal ptMoneyInByRegister(String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.ptMoneyInByRegister(utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal ptMoneyInBySettOut(String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.ptMoneyInBySettOut(utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public BigDecimal ptMoneyInByOrderPayType(String timelike, Integer ptype, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.ptMoneyInByOrderPayType(utils.getStartTime(), utils.getEndTime(), ptype);
    }

    @Override
    public CountRet findUserFirst() {
        List<CountRet> user = systemCountMapper.find_system_first_change_user();
        if (user != null) {
            return user.get(0);
        }
        return null;
    }

    @Override
    public BigDecimal count_tr_info(Integer type, String time, Long uid, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(time));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.count_tr_info(type, utils.getStartTime(), utils.getEndTime(), uid);
    }

    @Override
    public BigDecimal count_sale_money(Long uid, Integer status, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.todayOrderSaleCount(uid, status, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public List<ConnCountModel> cout_conn_today(String timelike, String endTime) {
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList<ConnCountModel> ret = new ArrayList<>();
        //易支付
        {
            List<EpayConfig> list = epayConfigMapper.selectOrderby1();
            for (EpayConfig config : list) {
                BigDecimal pay_money = new BigDecimal("0.00");
                BigDecimal unpay_money = new BigDecimal("0.00");
                Integer pay_order = 0;
                Integer unpay_order = 0;
                BigDecimal refunnd_money = new BigDecimal("0.00");
                BigDecimal change_sett = new BigDecimal("0.00");
                BigDecimal sale_money = new BigDecimal("0.00");
                List<SystemOrder> orders = systemOrderMapper.selectByPayConnetTypePayConnet(config.getId(), 0);
                for (SystemOrder order : orders) {
                    if (timelike != null && timelike.length() > 0) {
                        if (!time.format(order.getCreateTime()).equals(timelike)) {
                            continue;
                        }
                    }
                    Integer status = order.getStatus();
                    if (status == 1) {
                        unpay_money = unpay_money.add(order.getPrice());
                        unpay_order++;
                    } else if (status == 2) {
                        pay_money = pay_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal("0.00") : order.getChangeSett());
                        sale_money = sale_money.add(order.getSaleMonney() == null ? new BigDecimal("0.00") : order.getSaleMonney());
                        pay_order++;
                    } else if (status == 3) {
                        refunnd_money = refunnd_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal("0.00") : order.getChangeSett());
                    }
                }
                ConnCountModel model = new ConnCountModel();
                model.setEpay_name(config.getEpayName());
                model.setEpay_type(0);
                model.setConn_id(config.getId());
                model.setPay_money(pay_money);
                model.setUnpay_money(unpay_money);
                model.setPay_order(pay_order);
                model.setUnpay_order(unpay_order);
                model.setRefunnd_money(refunnd_money);
                model.setChange_sett(change_sett);
                model.setSale_money(sale_money);
                ret.add(model);
            }
        }
        //码支付
        {
            List<CodepayConfig> list = codepayConfigMapper.selectByStatusNameType(null, null, null);
            for (CodepayConfig config : list) {
                BigDecimal pay_money = new BigDecimal("0.00");
                BigDecimal unpay_money = new BigDecimal("0.00");
                Integer pay_order = 0;
                Integer unpay_order = 0;
                BigDecimal refunnd_money = new BigDecimal("0.00");
                BigDecimal change_sett = new BigDecimal("0.00");
                BigDecimal sale_money = new BigDecimal("0.00");
                List<SystemOrder> orders = systemOrderMapper.selectByPayConnetTypePayConnet(config.getId(), 1);
                for (SystemOrder order : orders) {
                    if (timelike != null && timelike.length() > 0) {
                        if (!time.format(order.getCreateTime()).equals(timelike)) {
                            continue;
                        }
                    }
                    Integer status = order.getStatus();
                    if (status == 1) {
                        unpay_money = unpay_money.add(order.getPrice());
                        unpay_order++;
                    } else if (status == 2) {
                        pay_money = pay_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                        sale_money = sale_money.add(order.getSaleMonney() == null ? new BigDecimal(0.00) : order.getSaleMonney());
                        pay_order++;
                    } else if (status == 3) {
                        refunnd_money = refunnd_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                    }
                }
                ConnCountModel model = new ConnCountModel();
                model.setEpay_name(config.getEpayName());
                model.setEpay_type(1);
                model.setConn_id(config.getId());
                model.setPay_money(pay_money);
                model.setUnpay_money(unpay_money);
                model.setPay_order(pay_order);
                model.setUnpay_order(unpay_order);
                model.setRefunnd_money(refunnd_money);
                model.setChange_sett(change_sett);
                model.setSale_money(sale_money);
                ret.add(model);
            }
        }
        //微信
        {
            List<WxpayConfig> list = wxpayConfigMapper.selectOrderBy();
            for (WxpayConfig config : list) {
                BigDecimal pay_money = new BigDecimal(0.00);
                BigDecimal unpay_money = new BigDecimal(0.00);
                Integer pay_order = 0;
                Integer unpay_order = 0;
                BigDecimal refunnd_money = new BigDecimal(0.00);
                BigDecimal change_sett = new BigDecimal(0.00);
                BigDecimal sale_money = new BigDecimal(0.00);
                List<SystemOrder> orders = systemOrderMapper.selectByPayConnetTypePayConnet(config.getId(), 2);
                for (SystemOrder order : orders) {
                    if (timelike != null && timelike.length() > 0) {
                        if (!time.format(order.getCreateTime()).equals(timelike)) {
                            continue;
                        }
                    }
                    Integer status = order.getStatus();
                    if (status == 1) {
                        unpay_money = unpay_money.add(order.getPrice());
                        unpay_order++;
                    } else if (status == 2) {
                        pay_money = pay_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                        sale_money = sale_money.add(order.getSaleMonney() == null ? new BigDecimal(0.00) : order.getSaleMonney());
                        pay_order++;
                    } else if (status == 3) {
                        refunnd_money = refunnd_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                    }
                }
                ConnCountModel model = new ConnCountModel();
                model.setEpay_name(config.getEpayName());
                model.setEpay_type(2);
                model.setConn_id(config.getId());
                model.setPay_money(pay_money);
                model.setUnpay_money(unpay_money);
                model.setPay_order(pay_order);
                model.setUnpay_order(unpay_order);
                model.setRefunnd_money(refunnd_money);
                model.setChange_sett(change_sett);
                model.setSale_money(sale_money);
                ret.add(model);
            }
        }
        //QQ
        {
            List<QqpayConfig> list = qqpayConfigMapper.selectByStatusNamePid(null, null, null);
            for (QqpayConfig config : list) {
                BigDecimal pay_money = new BigDecimal(0.00);
                BigDecimal unpay_money = new BigDecimal(0.00);
                Integer pay_order = 0;
                Integer unpay_order = 0;
                BigDecimal refunnd_money = new BigDecimal(0.00);
                BigDecimal change_sett = new BigDecimal(0.00);
                BigDecimal sale_money = new BigDecimal(0.00);
                List<SystemOrder> orders = systemOrderMapper.selectByPayConnetTypePayConnet(config.getId(), 3);
                for (SystemOrder order : orders) {
                    if (timelike != null && timelike.length() > 0) {
                        if (!time.format(order.getCreateTime()).equals(timelike)) {
                            continue;
                        }
                    }
                    Integer status = order.getStatus();
                    if (status == 1) {
                        unpay_money = unpay_money.add(order.getPrice());
                        unpay_order++;
                    } else if (status == 2) {
                        pay_money = pay_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                        sale_money = sale_money.add(order.getSaleMonney() == null ? new BigDecimal(0.00) : order.getSaleMonney());
                        pay_order++;
                    } else if (status == 3) {
                        refunnd_money = refunnd_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                    }
                }
                ConnCountModel model = new ConnCountModel();
                model.setEpay_name(config.getEpayName());
                model.setEpay_type(3);
                model.setConn_id(config.getId());
                model.setPay_money(pay_money);
                model.setUnpay_money(unpay_money);
                model.setPay_order(pay_order);
                model.setUnpay_order(unpay_order);
                model.setRefunnd_money(refunnd_money);
                model.setChange_sett(change_sett);
                model.setSale_money(sale_money);
                ret.add(model);
            }
        }
        //支付宝
        {
            List<AlipayConfig> list = alipayConfigMapper.selectByStatus(null);
            for (AlipayConfig config : list) {
                BigDecimal pay_money = new BigDecimal(0.00);
                BigDecimal unpay_money = new BigDecimal(0.00);
                Integer pay_order = 0;
                Integer unpay_order = 0;
                BigDecimal refunnd_money = new BigDecimal(0.00);
                BigDecimal change_sett = new BigDecimal(0.00);
                BigDecimal sale_money = new BigDecimal(0.00);
                List<SystemOrder> orders = systemOrderMapper.selectByPayConnetTypePayConnet(config.getId(), 4);
                for (SystemOrder order : orders) {
                    if (timelike != null && timelike.length() > 0) {
                        if (!time.format(order.getCreateTime()).equals(timelike)) {
                            continue;
                        }
                    }
                    Integer status = order.getStatus();
                    if (status == 1) {
                        unpay_money = unpay_money.add(order.getPrice());
                        unpay_order++;
                    } else if (status == 2) {
                        pay_money = pay_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                        sale_money = sale_money.add(order.getSaleMonney() == null ? new BigDecimal(0.00) : order.getSaleMonney());
                        pay_order++;
                    } else if (status == 3) {
                        refunnd_money = refunnd_money.add(order.getPrice());
                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
                    }
                }
                ConnCountModel model = new ConnCountModel();
                model.setEpay_name(config.getEpayName());
                model.setEpay_type(4);
                model.setConn_id(config.getId());
                model.setPay_money(pay_money);
                model.setUnpay_money(unpay_money);
                model.setPay_order(pay_order);
                model.setUnpay_order(unpay_order);
                model.setRefunnd_money(refunnd_money);
                model.setChange_sett(change_sett);
                model.setSale_money(sale_money);
                ret.add(model);
            }
        }
        //e交易
//        {
//            ElpayConfigExample example = new ElpayConfigExample();
//            example.setOrderByClause("order_by");
//            List<ElpayConfig> list = elpayConfigMapper.selectByExample(example);
//            for (ElpayConfig config : list) {
//                BigDecimal pay_money = new BigDecimal(0.00);
//                BigDecimal unpay_money = new BigDecimal(0.00);
//                Integer pay_order = 0;
//                Integer unpay_order = 0;
//                BigDecimal refunnd_money = new BigDecimal(0.00);
//                BigDecimal change_sett = new BigDecimal(0.00);
//                BigDecimal sale_money = new BigDecimal(0.00);
//                SystemOrderExample example1 = new SystemOrderExample();
//                SystemOrderExample.Criteria criteria = example1.createCriteria();
//                criteria.andPayConnetEqualTo(config.getId());
//                criteria.andPayConnetTypeEqualTo(6);
//                List<SystemOrder> orders = systemOrderMapper.selectByExample(example1);
//                for (SystemOrder order : orders) {
//                    if (timelike != null && timelike.length() > 0) {
//                        if (!time.format(order.getCreateTime()).equals(timelike)) {
//                            continue;
//                        }
//                    }
//                    Integer status = order.getStatus();
//                    if (status == 1) {
//                        unpay_money = unpay_money.add(order.getPrice());
//                        unpay_order++;
//                    } else if (status == 2) {
//                        pay_money = pay_money.add(order.getPrice());
//                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
//                        sale_money = sale_money.add(order.getSaleMonney() == null ? new BigDecimal(0.00) : order.getSaleMonney());
//                        pay_order++;
//                    } else if (status == 3) {
//                        refunnd_money = refunnd_money.add(order.getPrice());
//                        change_sett = change_sett.add(order.getChangeSett() == null ? new BigDecimal(0.00) : order.getChangeSett());
//                    }
//                }
//                ConnCountModel model = new ConnCountModel();
//                model.setEpay_name(config.getEpayName());
//                model.setEpay_type(6);
//                model.setConn_id(config.getId());
//                model.setPay_money(pay_money);
//                model.setUnpay_money(unpay_money);
//                model.setPay_order(pay_order);
//                model.setUnpay_order(unpay_order);
//                model.setRefunnd_money(refunnd_money);
//                model.setChange_sett(change_sett);
//                model.setSale_money(sale_money);
//                ret.add(model);
//            }
//        }
        return ret;
    }

    @Override
    public BigDecimal query_proxy_money_count(Long uid, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.query_proxy_count_money(uid, utils.getStartTime(), utils.getEndTime());
    }

    @Override
    public int query_proxy_user_count(Long uid, String timelike, String endTime) {
        DateUtils utils = new DateUtils();
        try {
            utils.setStart_time(DateUtils.format.parse(timelike));
            utils.setEnd_time(DateUtils.format.parse(endTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return systemCountMapper.query_proxy_count_user(uid, null, null);
    }

}
